﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessObject
{
    public class HocSinhDAO
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<HOCSINH> getAllHocSinhNoneMaHocSinh(NAMHOC namhoc)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var query = from data in entities.USERs.OfType<HOCSINH>()
                            where (from chitietlophoc in entities.CHITIETLOPHOCs
                                   where chitietlophoc.HOCSINHs_UserId == data.UserId &&
                                   chitietlophoc.LOPHOC.NamHocId == namhoc.NamHocId
                                   select chitietlophoc
                            ).Count<CHITIETLOPHOC>() <= 0
                            select data;
                return query.ToList();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lophoc"></param>
        /// <returns></returns>
        public List<HOCSINH> getAllHocSinhByLopHoc(LOPHOC lophoc)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var query = from data in entities.CHITIETLOPHOCs
                            where data.LOPHOCs_LopHocId == lophoc.LopHocId
                            select data.USERs_HOCSINH;
                return query.ToList();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hocsinh"></param>
        /// <param name="lophoc"></param>
        /// <returns></returns>
        public string insertHocSinh(HOCSINH hocsinh, LOPHOC lophoc)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                string result = string.Empty;
                var _hocsinh = entities.USERs.OfType<HOCSINH>().FirstOrDefault(p=>p.MaHocSinh== hocsinh.MaHocSinh);
                if (_hocsinh == null) result += "*Mã học sinh đã tồn tại!";
                if (result == string.Empty)
                {
                    entities.USERs.AddObject(hocsinh);
                    entities.SaveChanges();
                }
                return result;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hocsinh"></param>
        public void deleteHocSinh(HOCSINH hocsinh)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var _hocsinh = entities.USERs.OfType<HOCSINH>().FirstOrDefault(p => p.UserId == hocsinh.UserId);
                if (_hocsinh == null) return;
                entities.USERs.DeleteObject(_hocsinh);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hocsinh"></param>
        public string udpateHocSinh(HOCSINH hocsinh)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var _hocsinh = entities.USERs.OfType<HOCSINH>().FirstOrDefault(p => p.UserId == hocsinh.UserId);
                if (_hocsinh == null) return "*Gặp lõi trong quá trình tìm kiếm học sinh";
                _hocsinh.NguoiBaoHo = hocsinh.NguoiBaoHo;
                _hocsinh.NgayNghiHoc = hocsinh.NgayNghiHoc;
                _hocsinh.MaHocSinh = hocsinh.MaHocSinh;
                _hocsinh.GioiTinh = hocsinh.GioiTinh;
                _hocsinh.QueQuan = hocsinh.QueQuan;
                _hocsinh.DiaChi = hocsinh.DiaChi;
                entities.SaveChanges();
                return string.Empty;
            }
        }
    }
}
